﻿using System.Threading.Tasks;
using CommonManagement;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;

namespace CommonService.Host
{
    /// <summary>
    /// 公共服务数据种子
    /// </summary>
    public class CommonServiceDataSeeder : IDataSeedContributor, ITransientDependency
    {
        private readonly UserManager _userManager;
        private readonly IRepository<User, int> _userRepository;

        public CommonServiceDataSeeder(
            UserManager userManager,
            IRepository<User, int> userRepository)
        {
            _userManager = userManager;
            _userRepository = userRepository;
        }

        [UnitOfWork]
        public virtual async Task SeedAsync(DataSeedContext context)
        {
            await AddUsersAsync();
        }

        private async Task AddUsersAsync()
        {
            if (await _userRepository.AnyAsync(m => m.UserName == "test01"))
                return;
            await _userManager.CreateAsync(
                "test01",
                "123456",
                null,
                "测试用户 01",
                "0",
                "13800000000",
                "1jx1@qq.com",
                "210211199909090014");
        }
    }
}